package com.example.hosptial.repository;

import com.example.hosptial.model.enity.Nurse;
import com.example.hosptial.model.enity.NurseWardAssignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NurseRepository extends JpaRepository<Nurse, String> {
    
    // 根据部门ID查询护士
    @Query("SELECT n FROM Nurse n WHERE n.department.deptId = :deptId")
    List<Nurse> findByDepartmentId(@Param("deptId") Integer deptId);

    // 查询所有护士，并按照部门和姓名排序
    @Query("SELECT n FROM Nurse n LEFT JOIN FETCH n.department ORDER BY n.department.deptName, n.nurseName")
    List<Nurse> findAllWithDepartment();

    List<Nurse> findByDepartment_DeptId(Integer deptId);
    
    boolean existsByNurseIdCard(String nurseIdCard);
    
    boolean existsByNursePhone(String nursePhone);
    
    boolean existsByNurseId(String nurseId);

    // 查询护士的病房分配信息
    @Query("SELECT nwa FROM NurseWardAssignment nwa " +
           "LEFT JOIN FETCH nwa.ward w " +
           "LEFT JOIN FETCH w.floor f " +
           "WHERE nwa.nurse.nurseId = :nurseId")
    List<NurseWardAssignment> findNurseWardAssignmentsByNurseId(@Param("nurseId") String nurseId);

    @Query("SELECT DISTINCT f.floorNum FROM Floor f WHERE f.department.deptId = :deptId ORDER BY f.floorNum")
    List<Integer> findDistinctFloorsByDepartmentId(@Param("deptId") Integer deptId);
} 